using JT808Protocol.Models;
using System;
using System.Collections.Generic;

namespace JT808Protocol.Abstracts
{
    /// <summary>
    /// JT808消息处理器基础类
    /// </summary>
    public class MsgHandlerBase
    {
        /// <summary>
        /// 附件服务器IP
        /// </summary>
        public static string AlarmAttachmentServerIP { get; set; }

        /// <summary>
        /// 附件服务器端口
        /// </summary>
        public static int AlarmAttachmentServerPort { get; set; }

        /// <summary>
        /// 拦截类型
        /// </summary>
        public virtual EnumMessageType[] handleTypes { get; }

        /// <summary>
        /// 处理消息,返回1个到多个数据包
        /// </summary>
        /// <param name="reqPkg">设备请求包</param>
        public virtual JT808Package[] Execute(JT808Package reqPkg) => throw new NotImplementedException();

        /// <summary>
        /// 返回通用应答
        /// </summary>
        /// <param name="result">结果类型</param>
        public JT808Package[] CommonResult(JT808Package reqPkg, EnumResult result = EnumResult.成功)
        {
            return new[] { JT808PackageBuilder.PlatFormCommon.Build(reqPkg, result) };
        }

        /// <summary>
        /// 返回通用应答，加多个其他类型结果
        /// </summary>
        /// <param name="reqPkg">设备请求数据包</param>
        /// <param name="result">相应结果(1到多)</param>
        /// <param name="bodies">响应数据包体</param>
        /// <returns></returns>
        public JT808Package[] MultiResult(JT808Package reqPkg, EnumResult result = EnumResult.成功, params (EnumMessageType, IJT808Body)[] bodies)
        {
            List<JT808Package> results = new List<JT808Package>();
            var pkgCom = JT808PackageBuilder.PlatFormCommon.Build(reqPkg, result);
            results.Add(pkgCom);

            ushort serialNum = pkgCom.Header.SerialNumber;//流水号从上面通用应答基础上累加
            foreach (var body in bodies)
            {
                var pkg = new JT808Package((ushort)body.Item1, reqPkg.Header.SimNumber,
                    ++serialNum,
                    body.Item2,
                    Version: reqPkg.Header.ProtocolVersion);

                results.Add(pkg);
            }

            return results.ToArray();
        }

        /// <summary>
        /// 返回通用应答，加多个其他类型结果
        /// </summary>
        /// <param name="reqPkg">设备请求数据包</param>
        /// <param name="msgType">响应数据包体类型</param>
        /// <param name="bodies">响应数据包体</param>
        /// <param name="result">相应结果(1到多)</param>
        /// <returns></returns>
        public JT808Package[] MultiResult(JT808Package reqPkg, EnumMessageType msgType, IJT808Body[] bodies, EnumResult result = EnumResult.成功)
        {
            List<JT808Package> results = new List<JT808Package>();
            var pkgCom = JT808PackageBuilder.PlatFormCommon.Build(reqPkg, result);
            results.Add(pkgCom);

            ushort serialNum = pkgCom.Header.SerialNumber;//流水号从上面通用应答基础上累加
            foreach (var body in bodies)
            {
                var pkg = new JT808Package((ushort)msgType, reqPkg.Header.SimNumber,
                    ++serialNum,
                    body,
                    Version: reqPkg.Header.ProtocolVersion);

                results.Add(pkg);
            }

            return results.ToArray();
        }
    }
}